// =================================================================================================
//
//	CopyEngine Framework
//	Copyright 2012 Eran. All Rights Reserved.
//
//	This program is free software. You can redistribute and/or modify it
//	in accordance with the terms of the accompanying license agreement.
//
// =================================================================================================

package data.vo
{
    public class LayoutItemVoManger
    {
        private var mAllLayoutItemVoVector:Vector.<LayoutItemVo>;

        private static var _instance:LayoutItemVoManger;

        public static function get instance():LayoutItemVoManger
        {
            if (_instance == null)
            {
                _instance = new LayoutItemVoManger();
            }
            return _instance;
        }

        public function LayoutItemVoManger()
        {
            mAllLayoutItemVoVector = new Vector.<LayoutItemVo>();
        }


        //==================================
        //== Public Function
        //==================================

        //====================
        //== JSON 操作
        //====================

        public function fromJSON(_jsonObj:Object):void
        {
            mAllLayoutItemVoVector.length = 0;
            for each(var itemObj:Object in _jsonObj["itemArray"])
            {
                var newLayoutItem:LayoutItemVo = new LayoutItemVo();
                newLayoutItem.fromJSON(itemObj);
                mAllLayoutItemVoVector.push(newLayoutItem);
            }
        }

        public function toJSONString():String
        {
            if (mAllLayoutItemVoVector.length == 0)
            {
                return null;
            }
            else
            {
                try
                {
                    var itemArray:Array = [];
                    for each(var itemVo:LayoutItemVo in mAllLayoutItemVoVector)
                    {
                        itemArray.push(itemVo);
                    }

                    var jsonObj:Object = {};
                    jsonObj["itemArray"] = itemArray;

                    var jsonString:String = JSON.stringify(jsonObj);
                    return jsonString;
                } catch (e:Error)
                {
                    return "ERROR";
                }
            }
        }


        //====================
        //==ItemVo的增删改
        //====================

        public function get allLayoutItemVoVector():Vector.<LayoutItemVo> {return mAllLayoutItemVoVector;}

        public function createNewLayoutItem():LayoutItemVo
        {
            var item:LayoutItemVo = new LayoutItemVo();
            mAllLayoutItemVoVector.push(item);
            return item;
        }

        public function deleteLayoutItemByID(_itemID:String):void
        {
            for (var i:int = 0; i < mAllLayoutItemVoVector.length; i++)
            {
                var item:LayoutItemVo = mAllLayoutItemVoVector[i];
                if (item.itemID == _itemID)
                {
                    mAllLayoutItemVoVector.splice(i, 1);
                    break;
                }
            }
        }

        public function getLayoutItemByID(_itemID:String):LayoutItemVo
        {
            for (var i:int = 0; i < mAllLayoutItemVoVector.length; i++)
            {
                var item:LayoutItemVo = mAllLayoutItemVoVector[i];
                if (item.itemID == _itemID)
                {
                    return item;
                }
            }
            return null;
        }


    }
}
